package com.laikan.reader.bookfriend.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.facebook.imagepipeline.request.Postprocessor;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanApplication;
import jp.wasabeef.fresco.processors.BlurPostprocessor;

import java.io.InputStream;

/**
 * Created by admin on 2016/10/31.
 * 实现模糊背景处理的工具类，依赖与Fresco
 */
public class ImageUtils {

    /**
     * 设置view为高斯模糊效果
     *
     * @param draweeView
     * @param context
     * @param resId
     */
    public static void frescoBlur(SimpleDraweeView draweeView, Context context, int resId) {
        Postprocessor processor = new BlurPostprocessor(context, 30);
        ImageRequest request = ImageRequestBuilder.newBuilderWithResourceId(resId)
            .setPostprocessor(processor)
            .build();
        PipelineDraweeController controller = (PipelineDraweeController)
            Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(draweeView.getController())
                .build();

        draweeView.setController(controller);
    }

    /**
     * 设置view为高斯模糊效果
     *
     * @param draweeView
     * @param context
     * @param uri
     */
    public static void frescoBlur(SimpleDraweeView draweeView, Context context, String uri) {
        Postprocessor processor = new BlurPostprocessor(context, 30);
        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri))
            .setPostprocessor(processor)
            .build();
        PipelineDraweeController controller = (PipelineDraweeController)
            Fresco.newDraweeControllerBuilder()
                .setImageRequest(request)
                .setOldController(draweeView.getController())
                .build();

        draweeView.setController(controller);
    }

    /**
     * 提取图像Alpha位图并设置颜色
     *
     * @param mBitmap
     * @param mColor
     *
     * @return
     */
    public static Bitmap getAlphaBitmap(Bitmap mBitmap, int mColor) {
        //    	BitmapDrawable mBitmapDrawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.enemy_infantry_ninja);
        //    	Bitmap mBitmap = mBitmapDrawable.getBitmap();

        //BitmapDrawable的getIntrinsicWidth（）方法，Bitmap的getWidth（）方法
        //注意这两个方法的区别
        //Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmapDrawable.getIntrinsicWidth(), mBitmapDrawable.getIntrinsicHeight(), Config.ARGB_8888);
        Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas mCanvas = new Canvas(mAlphaBitmap);
        Paint mPaint = new Paint();

        mPaint.setColor(mColor);
        //从原位图中提取只包含alpha的位图
        Bitmap alphaBitmap = mBitmap.extractAlpha();
        //在画布上（mAlphaBitmap）绘制alpha位图
        mCanvas.drawBitmap(alphaBitmap, 0, 0, mPaint);

        return mAlphaBitmap;
    }

    /**
     * 以最省内存的方式读取本地资源的图片
     *
     * @param context
     * @param resId
     *
     * @return </br>修改为</br>
     * imageButton_fav.setImageBitmap(BitmapUtils.readBitMap(this,
     * R.drawable.guide_fav_1));
     */
    @SuppressWarnings ("deprecation")
    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 获取资源图片
        try {
            InputStream is = context.getResources().openRawResource(resId);
            return BitmapFactory.decodeStream(is, null, null);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 打开一个activity，并附加一个字段
     * @param clazz
     * @param key
     * @param value
     */
    public static void startActivity(Class clazz,String key,String value){
        Intent intent = new Intent();
        intent.putExtra(key,value);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setClass(ImageUtils.getContext(), clazz);
        ImageUtils.getContext().startActivity(intent);
    }

    /** dip转换px */
    public static int dip2px(int dip) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int)(dip * scale + 0.5f);
    }

    /**
     * px转换dip
     */
    public static int px2dip(int px) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int)(px / scale + 0.5f);
    }

    public static Context getContext() {
        return LaikanApplication.getContext();
    }

    public static BitmapDrawable getBlueBackButton() {
        BitmapDrawable drawable = new BitmapDrawable(ImageUtils.getAlphaBitmap(ImageUtils.readBitMap(getContext(), R.drawable.lk_back_small), Color.parseColor("#66ccdd")));
        return drawable;
    }


    /** 获取dimen */
    public static int getDimens(int resId) {
        return getContext().getResources().getDimensionPixelSize(resId);
    }}
